package loveappservice.controller;


import loveappservice.domain.Images;
import loveappservice.service.User_service;
import loveappservice.service.impl.User_service_impl;
import loveappservice.util.Result;
import loveappservice.util.WxApi;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class loginController {
    //实例化Service类
    User_service user_service = new User_service_impl();
    WxApi wxApi = new WxApi();

    /*微信登录查询*/
    @RequestMapping("/wxlogin")
    public Result wxlogin(String code){
        System.out.println("传输过来的code:" + code);
        //调取工具类获取openid
        String openid =  wxApi.code2Session(code);

        //查询该用户openid是否存在
        if (openid == "3001"|| openid == null){
            return Result.success(openid);   //返回3001 或者3002 表示查询openid失败

        }else {
            String username = user_service.openidLogin(openid);
            return Result.success(username);    //返回用户名，返回 3003表示该用户不在用户表里面
        }

    }

    /**
     * 用户名密码登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/userlogin")
    public Result userlogin(String username , String password) {
        System.out.println("username:"+username+","+"password:"+password);

        String res = user_service.userlogin(username,password);
        System.out.println(res);


        return Result.success(res);
    }

}
